// pages/cut/component/filter/filter.js
import {createFilter,addImageOnVideo} from '../../../../api/file'
import {getFileName,uploadFile} from '../../../../utils/request'
let originUrl = ''
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    uri: {
      type: String,
      default: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    currentFilter: 0,
    filters: [{value:1,label:'模糊'},{value:2,label:'黑白'},{value:3,label:'翻转'},{value:4,label:'倒放'}]
  },
  ready(){
    originUrl = this.data.uri
  },

  /**
   * 组件的方法列表
   */
  methods: {
    changeFilter(event){
      if (!this.data.uri) {
        wx.showModal({
          title: '提示',
          content: '请先选择视频'
        })
        return
      }
      const item = event.currentTarget.dataset.type
      const option = {
        fileName: getFileName(this.data.uri),
        type: item.value
      }
      wx.showLoading({
        title: '处理中',
      })
      createFilter(option).then(res => {
        wx.showToast({
          title: '处理完成',
          icon:'success'
        })
        this.triggerEvent("updateUri",{uri:res.msg})
      })
      this.setData({
        currentFilter: item.value
      })
    },
    resetVideo(){
      this.setData({
        currentFilter: 0
      })
      this.triggerEvent("updateUri",{uri:originUrl})
    },
    saveVideo(){
      const uri = this.data.uri
      wx.showLoading({
        title: '保存中',
      })
      wx.downloadFile({
        url: uri,
        success:(res)=>{
          uploadFile({
            filePath: res.tempFilePath
          }).then(res => {
            console.log(res)
            wx.showToast({
              title: '保存成功',
            })
          })
        }
      })
    },
    addImg(){
      if (!this.data.uri) {
        wx.showModal({
          title: '提示',
          content: '请先选择视频',
        })
        return
      }
      wx.chooseMedia({
        count:1,
        mediaType: 'image',
        success:(res)=>{
          const uri = res.tempFiles[0].tempFilePath
          wx.showLoading({
            title: '处理中',
            mask: true
          })
          uploadFile({
            filePath: uri
          }).then(res => {
            const option = {
              syImgNae: res.data,
              fileName: getFileName(this.data.uri)
            }
            addImageOnVideo(option).then(res => {
              wx.showToast({
                title: '处理完成',
                icon:'success'
              })
              this.triggerEvent("updateUri",{uri:res.data})
            })
          })
        }
      })
    }
  }
})